package com.example.basic.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: HttpAspect
 * Description:
 * date: 2021/12/14 15:38
 * author zesion lee
 */
@Aspect
@Component
@Configuration
@Slf4j
public class HttpAspect {

    @AfterReturning(value = "within(com.example.basic.controller.*)", returning = "rtv")
    public void AfterReturningImp(JoinPoint joinPoint, Object rtv) {

        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        //从获取RequestAttributes中获取HttpServletRequest的信息
        assert requestAttributes != null;
        HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
        if (request != null) {
            log.info("请求： Method:{} Url:{}, Result:{}", request.getMethod(), request.getRequestURI(), rtv.toString());
        }
    }

}
